package cn.zktr.Personnel.model.service.zhouyongcong;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zktr.Personnel.common.Md5Encode;
import cn.zktr.Personnel.common.TimeStamp;
import cn.zktr.Personnel.common.getIP;
import cn.zktr.Personnel.model.dao.ouyangpeng.RetWaltDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.AuditDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.AuditMdyDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.AuditTabDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.AuditZdDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsActDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsBzbDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsDeptDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsEmpDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsEmpdetDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsEmplxDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsHCesDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsLoginDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsMgmDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsPotDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsStmDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsYBunDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsYGDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsYQDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsYywxqDao;
import cn.zktr.Personnel.model.dao.zhouyongcong.PmsZtDao;
import cn.zktr.Personnel.model.pojos.PerAudit;
import cn.zktr.Personnel.model.pojos.PerAuditMdy;
import cn.zktr.Personnel.model.pojos.PerAuditTab;
import cn.zktr.Personnel.model.pojos.PerAuditZd;
import cn.zktr.Personnel.model.pojos.PerPmsAct;
import cn.zktr.Personnel.model.pojos.PerPmsBzb;
import cn.zktr.Personnel.model.pojos.PerPmsDept;
import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerPmsEmpdet;
import cn.zktr.Personnel.model.pojos.PerPmsEmplx;
import cn.zktr.Personnel.model.pojos.PerPmsFun;
import cn.zktr.Personnel.model.pojos.PerPmsHCes;
import cn.zktr.Personnel.model.pojos.PerPmsLogin;
import cn.zktr.Personnel.model.pojos.PerPmsMgm;
import cn.zktr.Personnel.model.pojos.PerPmsPes;
import cn.zktr.Personnel.model.pojos.PerPmsPot;
import cn.zktr.Personnel.model.pojos.PerPmsSex;
import cn.zktr.Personnel.model.pojos.PerPmsStm;
import cn.zktr.Personnel.model.pojos.PerPmsYBun;
import cn.zktr.Personnel.model.pojos.PerPmsYG;
import cn.zktr.Personnel.model.pojos.PerPmsYQ;
import cn.zktr.Personnel.model.pojos.PerPmsYYwxq;
import cn.zktr.Personnel.model.pojos.PerPmsZt;
import cn.zktr.Personnel.model.pojos.PerRetWAlt;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitgetService;

@Service
@Transactional(rollbackFor=Exception.class)
public class saveService {
	@Autowired
	private findService fs;
	@Autowired
	private monitgetService mgs;
	@Autowired
	private PmsYQDao pqd;
	@Autowired
	private PmsYGDao pgd;
	@Autowired
	private PmsYBunDao pybd;
	@Autowired
	private PmsMgmDao pmd;
	@Autowired
	private PmsLoginDao pld;
	@Autowired
	private PmsDeptDao pdd;
	@Autowired
	private PmsEmpDao ped;
	@Autowired
	private PmsPotDao ppd;
	@Autowired
	private PmsBzbDao pbd;
	@Autowired
	private RetWaltDao rwd;
	@Autowired
	private PmsYywxqDao pyyd;
	@Autowired 
	private AuditTabDao atd;
	@Autowired 
	private AuditZdDao azd;
	@Autowired
	private PmsEmpdetDao pedd;
	@Autowired
	private AuditDao ad;
	@Autowired
	private AuditMdyDao amd;
	@Autowired
	private PmsEmplxDao peld;
	@Autowired
	private PmsStmDao psd;
	@Autowired
	private PmsActDao pad;
	@Autowired
	private PmsZtDao pzd;
	@Autowired
	private PmsHCesDao phcd;
	
	public void save_HCes(int cesid,Integer empid,Integer hcontid) {
		PerPmsHCes pphc = new PerPmsHCes();
		pphc.setCesid(cesid);
		pphc.setPerPmsEmp(fs.findEmpid(empid));
		pphc.setPerPmsHCont(fs.findHContid(hcontid));
		
		Calendar a=Calendar.getInstance();
		Timestamp time = new TimeStamp().getNetworkTime();
		int year = time.getYear()+1900;
		int month = time.getMonth()+1;
		int date = time.getDate();
		System.err.println(year+"-"+month+"-"+date);
		pphc.setCesqtime(new TimeStamp().valueOf(year+"-"+month+"-"+date));
		pphc.setCesdtime(new TimeStamp().valueOf(year+1+"-"+month+"-"+date));
		phcd.save(pphc);
	}
	
	public void save_Zt(Integer empid,String Zthost) {
		PerPmsZt ppz = new PerPmsZt();
		ppz.setPerPmsEmp(fs.findEmpid(empid));
		ppz.setZtid(empid);
		ppz.setZtzts("255.255.255");
		ppz.setZtback("url(../static/zyc/images/back-image/背景图-002.jpg)");
		ppz.setZthost(Zthost);
		pzd.save(ppz);
	}
	
	public void save_Act(Integer empid) {
		PerPmsAct ppa = new PerPmsAct();
		ppa.setActid(empid);
		ppa.setPerPmsEmp(fs.findEmpid(empid));
		ppa.setActpwd(new Md5Encode().MD5Encode("123456"));
		ppa.setActaccount("113355"+empid);
		pad.save(ppa);
	}
	
	public void save_Stm(Integer empid,Integer potid,Integer deptid) {
		PerPmsStm pps = new PerPmsStm();
		pps.setPerPmsDept(fs.findDeptid(deptid));
		pps.setPerPmsEmp(fs.findEmpid(empid));
		pps.setPerPmsPot(fs.findPotid(potid));
		psd.save(pps);
	}
	
	public void save_Emplx(Integer lxxxid,Integer empid,Integer lxfsid,String lxxx) {
		PerPmsEmplx ppel = new PerPmsEmplx();
		ppel.setLxid(lxxxid);
		ppel.setLxphone(lxxx);
		ppel.setPerPmsEmp(fs.findEmpid(empid));
		ppel.setPerPmsLxf(fs.findLxfsid(lxfsid));
		peld.save(ppel);
	}
	
	public void save_Mdy(PerAuditMdy pam) {
		amd.save(pam);
	}
	
	public void save_Mdy(int mdyid,PerAuditTab pat,PerAuditZd paz,PerAudit pa,String mmname,int mmid,Object new_text,Object old_text) {
		PerAuditMdy pam = new PerAuditMdy();
		pam.setMdyid(mdyid);
		pam.setPerAuditTab(pat);
		pam.setPerAuditZd(paz);
		pam.setPerAudit(pa);
		pam.setMdymdyidname(mmname);
		pam.setMdymdyid(mmid);
		pam.setMdynr(new_text+"");
		pam.setMdynrs(old_text+"");
		pam.setMdytime(new TimeStamp().getNetworkTime());
		amd.save(pam);
	}
	
	public void save_Mdy(int mdyid,PerAuditTab pat,PerAuditZd paz,PerAudit pa,String mmname,int mmid,Object text) {
		PerAuditMdy pam = new PerAuditMdy();
		pam.setMdyid(mdyid);
		pam.setPerAuditTab(pat);
		pam.setPerAuditZd(paz);
		pam.setPerAudit(pa);
		pam.setMdymdyidname(mmname);
		pam.setMdymdyid(mmid);
		pam.setMdynr(text+"");
		pam.setMdytime(new TimeStamp().getNetworkTime());
		amd.save(pam);
	}
	
	public int save_Audit_id(int shlxid1,int empid,String auditsj,String auditrem) {
		PerAudit pa = new PerAudit();
		pa.setPerAuditShlx2(fs.findShlxid(shlxid1));
		pa.setPerPmsEmp1(fs.findEmpid(empid));
		pa.setPerPmsEmp2(mgs.getPerPmsEmp(fs.findEmpid(empid),fs.findShlxid(shlxid1)));
		pa.setAuditsj(auditsj);
		pa.setAudittime(new TimeStamp().getNetworkTime());
		pa.setAuditjg("待处理");
		pa.setAuditrem(auditrem);
		ad.save(pa);
		List<PerAudit> list = (List<PerAudit>) ad.findAll();
		return list.get(list.size()-1).getAuditid();
	}
	
	public int save_Audit_id(int shlxid1,int shlxid2,int empid,String auditsj,String auditrem) {
		PerAudit pa = new PerAudit();
		pa.setPerAuditShlx1(fs.findShlxid(shlxid2));
		pa.setPerAuditShlx2(fs.findShlxid(shlxid1));
		pa.setPerPmsEmp1(fs.findEmpid(empid));
		pa.setPerPmsEmp2(mgs.getPerPmsEmp(fs.findEmpid(empid),fs.findShlxid(shlxid1)));
		pa.setAuditsj(auditsj);
		pa.setAudittime(new TimeStamp().getNetworkTime());
		pa.setAuditjg("待处理");
		pa.setAuditrem(auditrem);
		ad.save(pa);
		List<PerAudit> list = (List<PerAudit>) ad.findAll();
		return list.get(list.size()-1).getAuditid();
	}
	
	public void save_Empdet(PerPmsSex perPmsSex,int empid,int detage,String detmz,String detgj,Timestamp detsr,String detzz) {
		PerPmsEmpdet pped = new PerPmsEmpdet();
		pped.setPerPmsSex(perPmsSex);
		pped.setPerPmsEmp(fs.findEmpid(empid));
		pped.setDetage(detage);
		pped.setDetmz(detmz);
		pped.setDetgj(detgj);
		pped.setDetsr(detsr);
		pped.setDetzz(detzz);
		pedd.save(pped);
	}
	
	public void save_Zd(PerAuditZd paz) {
		azd.save(paz);
	}
	
	public void save_Zd(String ztname,Integer ztid,PerAuditTab pat) {
		PerAuditZd paz = new PerAuditZd();
		paz.setZtid(ztid);
		paz.setZtname(ztname);
		paz.setPerAuditTab(pat);
		azd.save(paz);
	}
	
	public void save_Tab(PerAuditTab pat) {
		atd.save(pat);
	}
	
	public void save_Tab(String tabname,Integer tabid) {
		PerAuditTab pat = new PerAuditTab();
		pat.setTabid(tabid);
		pat.setTabname(tabname);
		atd.save(pat);
	}
	
	public void save_Yywxq(int bunid,int empid) {
		PerPmsYYwxq ppyy = new PerPmsYYwxq();
		ppyy.setPerPmsYBun(fs.findBunid(bunid));
		ppyy.setPerPmsEmp(fs.findEmpid(empid));
		pyyd.save(ppyy);
	}
	
	public void save_Walt(int waltid,int potid,int potsum,String altf,int emp1id,int emp2id,int deptid,int zjid,String altst) {
		PerRetWAlt prw = new PerRetWAlt();
		prw.setAltid(waltid);
		prw.setPerPmsPot(fs.findPotid(potid));
		prw.setAltnum(potsum);
		prw.setAltfw("对内");//对内or对外
		prw.setAltmintime(new TimeStamp().getNetworkTime());
		prw.setPerPmsEmp1(fs.findEmpid(emp1id));
		prw.setPerPmsEmp2(fs.findEmpid(emp2id));
		prw.setPerPmsDept(fs.findDeptid(deptid));
		prw.setPerPmsZJnxx(fs.findZJnxxid(zjid));
		prw.setAltstat("1");//状态
		rwd.save(prw);
	}
	
	public void save_Bzb(int bzbid,int deptid,int potpid,int bzbsum) {
		PerPmsBzb ppb = new PerPmsBzb();
		ppb.setBzbiid(bzbid);
		ppb.setPerPmsDept(fs.findDeptid(deptid));
		ppb.setPerPmsPot(fs.findPotid(potpid));
		ppb.setBzbnum(bzbsum);
		pbd.save(ppb);
	}
	
	public int save_Dept_id(String deptname,int deptid,String depttext) {
		PerPmsDept ppd = new PerPmsDept();
		ppd.setDeptname(deptname);
		ppd.setDeptss(deptid);
		ppd.setDepttime(new TimeStamp().getNetworkTime());
		ppd.setDeptzy(depttext);
		pdd.save(ppd);
		List<PerPmsDept> list = (List<PerPmsDept>) pdd.findAll();
		return list.get(list.size()-1).getDeptid();
	}
	
	public void save_Dept(String deptname,int deptid,String depttext) {
		PerPmsDept ppd = new PerPmsDept();
		ppd.setDeptname(deptname);
		ppd.setDeptss(deptid);
		ppd.setDepttime(new TimeStamp().getNetworkTime());
		ppd.setDeptzy(depttext);
		pdd.save(ppd);
	}
	
	public void save_Pot(int potid,int zjiid,String potname) {
		PerPmsPot ppp = new PerPmsPot();
		ppp.setPotid(potid);
		ppp.setPotzw(potname);
		ppp.setPerPmsZhiji(fs.findZhiji(zjiid));
		ppd.save(ppp);
	}
	
	public int save_Pot_id(int potid,int zjiid,String potname) {
		PerPmsPot ppp = new PerPmsPot();
		ppp.setPotid(potid);
		ppp.setPotzw(potname);
		ppp.setPerPmsZhiji(fs.findZhiji(zjiid));
		ppd.save(ppp);
		List<PerPmsPot> list = (List<PerPmsPot>) ppd.findAll();
		return list.get(list.size()-1).getPotid();
	}
	
	public int save_Emp_id(String empname,String empcard) {
		PerPmsEmp ppe = new PerPmsEmp();
		ppe.setEmpname(empname);
		ppe.setEmpcard(empcard);
		ped.save(ppe);
		List<PerPmsEmp> list = (List<PerPmsEmp>) ped.findAll();
		return list.get(list.size()-1).getEmpid();
	}
	
	public void save_Emp(String empname,String empcard) {
		PerPmsEmp ppe = new PerPmsEmp();
		ppe.setEmpname(empname);
		ppe.setEmpcard(empcard);
		ped.save(ppe);
	}
	
	public void save_Login(int id,HttpServletRequest request) {
		PerPmsLogin ppl = new PerPmsLogin();
		ppl.setPerPmsEmp(fs.findEmpid(id));
		ppl.setLogintime(new TimeStamp().getNetworkTime());
		ppl.setLoginnet(new getIP().getIpAddr(request));
		pld.save(ppl);
	}
	
	public void save_Mgm(int dept,int empid,String mgm) {
		PerPmsMgm ppm = new PerPmsMgm();
		ppm.setPerPmsDept(fs.findDeptid(dept));
		ppm.setPerPmsEmp(fs.findEmpid(empid));
		ppm.setMgmyq(mgm);
		pmd.save(ppm);
	}
	
	public int save_YBun_id(int deptid,int empid,int bunlxid,String deptname,int bunsum) {
		PerPmsYBun ppyb = new PerPmsYBun();
		ppyb.setPerPmsDept(fs.findDeptid(deptid));
		ppyb.setPerPmsEmp(fs.findEmpid(empid));
		ppyb.setPerPmsYYtyp(fs.findYYtypid(bunlxid));
		ppyb.setBuntime(new TimeStamp().getNetworkTime());
		ppyb.setBunname(deptname);
		ppyb.setBunber(bunsum);
		pybd.save(ppyb);
		List<PerPmsYBun> list = (List<PerPmsYBun>) pybd.findAll();
		return list.get(list.size()-1).getBunid();
	}
	
	public void save_YBun(int deptid,int empid,int bunlxid,String deptname,int bunsum) {
		PerPmsYBun ppyb = new PerPmsYBun();
		ppyb.setPerPmsDept(fs.findDeptid(deptid));
		ppyb.setPerPmsEmp(fs.findEmpid(empid));
		ppyb.setPerPmsYYtyp(fs.findYYtypid(bunlxid));
		ppyb.setBuntime(new TimeStamp().getNetworkTime());
		ppyb.setBunname(deptname);
		ppyb.setBunber(bunsum);
		
		pybd.save(ppyb);
	}
	
	public void save_YG(int id, String sf) {
		PerPmsYG ppyg = fs.findYGid(id);
		ppyg.setYgwht(sf);
		pgd.save(ppyg);
	}
	
	public void save_YQ(int id, String sf) {
		PerPmsYQ ppyq = fs.findYQid(id);
		ppyq.setYqwht(sf);
		pqd.save(ppyq);
	}
	
	public void save_YG(int ygid,PerPmsEmp ppe,PerPmsFun ppf, String sf) {
		PerPmsYG ppyg = new PerPmsYG();
		ppyg.setYgid(ygid);
		ppyg.setPerPmsEmp(ppe);
		ppyg.setPerPmsFun(ppf);
		ppyg.setYgwht(sf);
		pgd.save(ppyg);
	}
	
	public void save_YQ(int yqid,PerPmsEmp ppe,PerPmsPes ppp, String sf) {
		PerPmsYQ ppyq = new PerPmsYQ();
		ppyq.setYqid(yqid);
		ppyq.setPerPmsEmp(ppe);
		ppyq.setPerPmsPe(ppp);
		ppyq.setYqwht(sf);
		pqd.save(ppyq);
	}
}
